package com.ncs.cns.business.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;

import com.ncs.cns.business.constants.CookieConstants;
import com.ncs.cns.business.dao.impl.EventDAO;
import com.ncs.cns.business.dao.impl.ResourceDAO;
import com.ncs.cns.business.to.Event;
import com.ncs.cns.business.util.DateUtil;

public class EventService {

	private EventDAO eventDAO;

	private ResourceDAO resourceDAO;

	public EventService(Context context) {
		eventDAO = new EventDAO(context);
		resourceDAO = new ResourceDAO(context);
	}

	public List<Event> findByExpireDate(int offset, int pageLimit) {
		String dateStr = DateUtil.formatDate(getExpiredDate(), DateUtil.yyyy_MM_dd_HH_mm_ss);
		return eventDAO.findByPage(offset, pageLimit);
	}
	
	public List<Event> findByPageForMy(int offset, int pageLimit) {
		return eventDAO.findByPage(offset, pageLimit,"and IS_MY = 'Y'");
	}

	public boolean isExist(Integer eventId) {
		return eventDAO.get(eventId)==null;
	}

	public void add(Event entity) {
		eventDAO.insert(entity);	
	}
	
	public Event getEvent(int id){
		return  eventDAO.get(id);
	}
	
	public void updateEventStatusById(int id,String status){
		Event e = eventDAO.get(id);
		e.setEventStatus(status);
		eventDAO.update(e);
	}
	
	public void updateEventIsReadById(int id, String isRead){
		Event e = eventDAO.get(id);
		e.setIsRead(isRead);
		eventDAO.update(e);
	}
	
	public void delete(int id){
		eventDAO.delete(id);
	}

	public Date getExpiredDate(){
		int iMaxInactiveDurationInMin = CookieConstants.EVENT_DEFAULT_CLOSING_INTERVAL;
        Calendar oRSCalendar = Calendar.getInstance();
        oRSCalendar.add(Calendar.MINUTE, iMaxInactiveDurationInMin*(-1));
        Date expiredDate = oRSCalendar.getTime();
        return expiredDate;
	}
	
	public void updateWatchNumber(Event event){
		Event e = eventDAO.get(event.getEventId());
		e.setEventNumberWatched(event.getEventNumberWatched());
		eventDAO.update(e);
	}

	public void updateGood(Event event) {
		Event e = eventDAO.get(event.getEventId());
		e.setEventNumberGood(event.getEventNumberGood());
		eventDAO.update(e);
	}

	public void updateEventIsGood(Integer id, String isGood) {
		Event e = eventDAO.get(id);
		e.setIsPraise(isGood);
		eventDAO.update(e);
	}
}
